<?php

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\model\StReservation as StReservationModel;
use app\api\service\R;
use app\common\model\StReservationAttr;
use app\common\model\StUser as StUserModel;
use think\facade\Request;
use function Composer\Autoload\includeFile;

class StReservation extends Base
{
    public function StReservationList(){

    }
    public function addStreservation(Request $request){
        if (!$this->request->isPost()) return R::error();
        $data = $request::getInput();
        $data = $this->twoDimensionalArray($data);
//        $result = $this->validate($data,'StReservation');
//        if(true !== $result) $this->error($result);

        $re = (new StReservationModel)->addStReservation($data);
        if($re!==true) return R::error("$re");
        return R::success();
    }

    public function updateStreservation(Request $request){
        if (!$this->request->isPost()) return R::error();
        $data = $request::getInput();
        $data = $this->twoDimensionalArray($data);
        //$re = (new StReservationModel)->addStReservation($data);
        //if($re!==true) return R::error("$re");
        return R::success();

    }

    /**取消
     * parent全部取消 传id
     * children部分取消 传id
     * @param Request $request
     * @return \think\response\Json
     */
    public function cancelStreservation(Request $request){
        if (!$this->request->isPost()) return R::error();
        $data = $request::getInput();
        $data = json_decode($data, true);
       // dump($data);exit();
        if (isset($data['parent'])&&$data['parent']!=''){
            StReservationModel::where(['id'=>$data['parent']])->setField('status',0);
            $re=StReservationAttr::where(['mid'=>$data['parent']])->setField('attr_status',2);
        }
        if (isset($data['children'])&&$data['children']!='') {
            $re=StReservationAttr::where(['id'=>$data['children']])->setField('attr_status',2);
        }
        if($re!=true) return R::error("$re");
        return R::success();
    }

    /**删除预定
     * @param Request $request
     * @return \think\response\Json
     */
    public function deleteaStreservation(Request $request){
        if ($this->request->isDelete()) {
            $reservaId = $request::param('reservaId');
            if($reservaId==null) '请求参数异常';
            if (StReservationModel::destroy(['id'=>$reservaId,'comid'=>COMID])) {
                return R::success();
            };
        }
        return R::error();
    }
}